//
//  Aspect-Window.m
//  rssreader
//
//  Created by Zzz on 15/11/29.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "AppDelegate.h"
#import <XAspect/XAspect.h> // aop 面向切片
#import "BaseTabBar.h"
#import "ZXTestView.h"
#import "NavStatus.h"
//#import "RegiestWXViewController.h"
#import "BaseNav.h"
#import "UIView+UIView_ITAdditions.h"
#import "LaunchVC.h"
#import "AppStatus.h"
#import "VerifyPhoneViewController.h"
//#import "AppDatabase.h"

#define AtAspect Window

#define AtAspectOfClass AppDelegate
@classPatchField(AppDelegate)

AspectPatch(-, void,application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions) {
    NSLog(@"122222");
    //数据库初始化设定
//    self.database = [[AppDatabase alloc]initWithMigrations];
    //网络连接
//    @"http://dev.beadmall.fetion.cn/v1/api.ashx"
    //正式
    [Action actionConfigHost:SERVICE_LITTLE_ADDRESS client:@"" codeKey:@"result" rightCode:1 msgKey:@"errormsg"];
    //测试
//    [Action actionConfigHost:@"114.215.88.28" client:@"" codeKey:@"result" rightCode:1 msgKey:@"errormsg"];
    
    // 普通的window
    BaseTabBar *centerNav = [[BaseTabBar alloc]init];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = centerNav;
    [self.window makeKeyAndVisible];
    // 登录的window
    
    VerifyPhoneViewController * loginVC = [[VerifyPhoneViewController alloc]init];
    BaseNav * regiestNav = [[BaseNav alloc]initWithRootViewController:loginVC];

    self.loginWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.loginWindow.backgroundColor = [UIColor redColor];
    self.loginWindow.rootViewController = regiestNav;
    
    self.launchWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    BaseNav *launchNav = [[BaseNav alloc]initWithRootViewController:[[LaunchVC alloc] init]];
    self.launchWindow.backgroundColor = [UIColor whiteColor];
    self.launchWindow.rootViewController = launchNav;
    if([AppStatus judgeAPPFristLaunch]){
        [self.launchWindow makeKeyAndVisible];
    }
//    [self.loginWindow makeKeyAndVisible];
    // 奢侈window的初始层级
    [self.loginWindow setHidden:YES];
    [self.window setHidden:NO];
    // 网络监听
    [[$ rac_didNetworkChanges]
     subscribeNext:^(NSNumber *status) {
         AFNetworkReachabilityStatus networkStatus = [status intValue];
         switch (networkStatus) {
             case AFNetworkReachabilityStatusUnknown:
                 break;
             case AFNetworkReachabilityStatusNotReachable:
                 [[DialogUtil sharedInstance] showDlg:self.window textOnly:@"网络连接不给力"];
                 break;
             case AFNetworkReachabilityStatusReachableViaWWAN:
                 [[DialogUtil sharedInstance] showDlg:self.window textOnly:@"当前使用移动数据网络"];
                 break;
             case AFNetworkReachabilityStatusReachableViaWiFi:
                 break;
         }
     }];
    
    //添加testView
#ifdef kZXTestViewOpen
    ZXTestView * testView = [ZXTestView loadBundleNib];
    [testView setFrame_width:SCREEN_WIDTH];
    [self.window addSubview:testView];
#endif
    [[NavStatus sharedInstance]setAppdelegate:self];
    XAMessageForward(application:application didFinishLaunchingWithOptions:launchOptions);
}
@end
#undef AtAspectOfClass

#undef AtAspect
